from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from base.logger import logger
from selenium.webdriver.common.action_chains import ActionChains


class BasePage():

    def __init__(self, driver):
        """
        实现浏览器的初始化
        """
        logger.info("开始初始化浏览器驱动。。。。")
        self.driver = driver
        logger.info("设置隐式等待时长：10s")
        self.driver.implicitly_wait(10)
        logger.info("设置浏览器最大化。。。。")
        self.driver.maximize_window()

    def my_get(self, url):
        """
        打开指定url方法封装
        :param url:
        :return:
        """
        logger.info("打开页面url为：{}".format(url))
        try:
            self.driver.get(url)
        except:
            logger.error("url页面打开失败，请检查url是否正确。。")
            raise Exception("url打开失败。。。")

    def my_find_element(self, ele, time_out=10):
        """
        查找元素方法封装
        :param ele: 元素定位，传入元组，例如：('id', 'kw')
        :param time_out: 超时时间，默认10s
        :return:
        """
        logger.info("查找元素，元素定位方式：{}，元素定位超时时间为：{}s".format(ele, time_out))
        self.element = WebDriverWait(self.driver, time_out, 0.5).until(EC.presence_of_element_located(ele))

    def my_click(self, ele, **kwargs):
        """
        元素点击方法封装
        :param ele: 元素定位，传入元组，例如：('id', 'kw')
        :param kwargs: 可传参设置元素定位超时时间：time_out
        :return:
        """
        self.my_find_element(ele, **kwargs)
        logger.info("点击此元素。。。。")
        self.element.click()

    def my_send_keys(self, ele, value, time_out=10):
        """
        元素内输入文本的方法封装
        :param ele:
        :param value:
        :param time_out:
        :return:
        """
        self.my_find_element(ele, time_out)
        logger.info("元素内输入文本：{}".format(value))
        self.element.send_keys(value)

    def my_get_cookies(self):
        """
        获取浏览器所有cookie信息
        :return:
        """
        cookies = self.driver.get_cookies()
        logger.info("当前cookies信息为：{}".format(cookies))
        return cookies

    def my_set_cookies(self, cookies):
        """
        设置cookies方法封装
        :param cookies: cookie列表
        :return:
        """
        logger.info("设置cookies绕过登录。。。。")
        if type(cookies) == list:
            for cookie in cookies:
                if type(cookie) == dict:
                    self.driver.add_cookie(cookie)
                else:
                    logger.error("cookie非字典，错误cookie：{}".format(cookie))
                    raise Exception("cookie非字典")
        else:
            logger.error("cookies类型非列表类型，请确认后再试，cookies：{}".format(cookies))
            raise Exception("cookies类型非列表类型")

    def my_get_element_text(self, ele):
        """
        获取元素文本值方法封装
        :param ele:
        :return:
        """
        self.my_find_element(ele)
        ele_text = self.element.text
        logger.info("元素文本值为：{}".format(ele_text))
        return ele_text

    def my_move_to_element(self, ele):
        """
        鼠标移动
        :param ele:
        :return:
        """
        self.my_find_element(ele)
        ActionChains(self.driver).move_to_element(self.element).perform()

if __name__ == '__main__':

    base_page = BasePage()
    # 进入企业微信首页
    base_page.my_get("https://work.weixin.qq.com/")
    # 点击-企业登录
    base_page.my_click(ele=(By.CLASS_NAME, "index_top_operation_loginBtn"))
    # time.sleep(30)
    # cookies = base_page.my_get_cookies()
    # print(cookies)
    cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.logined', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'true'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.cs_ind', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1688858211371146'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1970325261984975'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1688858211371146'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'a9853058'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '069111'}, {'domain': '.work.weixin.qq.com', 'expiry': 1689336085, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'zh'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '0_YAt33GQKvXVLhjBYJDz-ogohH3INyeGe_EYj5DbfzNcUsaZCB7FtLlm6LPAQLNgaC4pp9ZgoHPFWgzDsK_0sFMRLYfta29sDgt-4sKrjmUXBqB4tJ6HsOqQ9-GxvPkjkBJuSGIGNFKe1O-H33XteDnuzTaNbP5FzxQByBlspX9n4WTJMhLf8fhk4a1IEewb66aOY0iXhevd7VJzncWjgP5-4ViNoLDGAmNejr2B0Ww1HzxIVGhpPn6SKotDXuWzZHOJzmYcBThW8e4DUvRSQ'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'lanyrmqDIWwVSFavtSOE4ds9RHfX35scLwl0E0e6y85o4p9w-tqAWicp8Z3jNb1D'}, {'domain': '.work.weixin.qq.com', 'expiry': 1718280069, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '0'}]
    # 设置cookies绕过登录
    base_page.my_set_cookies(cookies)
    time.sleep(5)